package com.rykj.qxj.server.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rykj.qxj.server.common.utils.CollectionUtils;
import com.rykj.qxj.server.dao.DataMartMapper;
import com.rykj.qxj.server.model.entity.DataMart;
import com.rykj.qxj.server.services.IDataMartService;
import com.techindata.cacheredis.CacheZSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("dataMartService")
public class DataMartServiceImpl implements IDataMartService {

    @Autowired
    private DataMartMapper dataMartMapper;

    @Override
    public void saveDataMart(DataMart dataMart) throws Exception{
        dataMartMapper.saveDataMart(dataMart);
    }

    @Override
    public void updateDataMart(DataMart dataMart) throws Exception{
        dataMartMapper.updateDataMart(dataMart);
    }

    @Override
    public void deleteDataMart(Integer dataMartId) throws Exception{
        dataMartMapper.deleteDataMart(dataMartId);
    }

    @Override
    public List<Map<String, Object>> getDataMartListTree(int parentId,String classLib,String martName) {
        Map<String,Object> paraMap= new HashMap<>();
        if("%".equals(martName)){
            martName ="\\%";
        }
        paraMap.put("parentId",parentId);
        paraMap.put("classLib",classLib);
        paraMap.put("martName",martName);
        List<Map<String, Object>> dataMarts =  dataMartMapper.getDataMartListTree(paraMap);
        if(!CollectionUtils.isEmpty(dataMarts)){
            for (Map<String, Object>  dataMart:dataMarts ) {
                paraMap.put("parentId",Integer.valueOf(dataMart.get("id")+""));
                List<Map<String,Object>> retChilds= dataMartMapper.getDataMartListTree(paraMap);
                dataMart.put("children",retChilds);
                if(CollectionUtils.isNotEmpty(retChilds)){
                    Integer apiCount=0;
                    for (Map<String,Object> ret:retChilds) {
                        apiCount+=Integer.valueOf(ret.get("apiCount")+"");
                    }
                    dataMart.put("apiCount",apiCount);
                }
            }
        }
        return  dataMarts;
    }

    @Override
    public PageInfo getDataMartList(Integer pageNum, Integer pageSize, String classLib, String martName) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> paraMap = new HashMap<>();
        if("%".equals(martName)){
            martName ="\\%";
        }
        paraMap.put("classLib",classLib);
        paraMap.put("martName",martName);
        List<Map<String,Object>> dataMarts =  dataMartMapper.getDataMartList(paraMap);
        PageInfo pageResult = new PageInfo(dataMarts);
        return  pageResult;

    }

    @Override
    public Integer getCountApiTotal(String classLib, Integer catalogId) {
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("classLib",classLib);
        paraMap.put("catalogId",catalogId);
        return dataMartMapper.getCountApiTotal(paraMap);
    }

    @Override
    public Integer getThisMonthApiTotal(String classLib, Integer catalogId) {
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("classLib",classLib);
        paraMap.put("catalogId",catalogId);
        return dataMartMapper.getThisMonthApiTotal(paraMap);
    }

    @Override
    public List<Map<String, Object>> getDataMartCount(Map<String, Object> paraMap) {
        return dataMartMapper.getDataMartCount(paraMap);
    }

	@Override
	public DataMart selectDataMart(Integer id) {
		return dataMartMapper.selectDataMart(id);
	}

	//接口统计key
    private static final String PER_API_VISITCOUNT= "perApiVisitCount";
    @Override
    public List<Map<String, Object>> getCatalogCountTopTen(Map<String, Object> paraMap) {
        List<Map<String,Object>> apiInfos = dataMartMapper.getCatalogCountTopTen(paraMap);
        if(!CollectionUtils.isEmpty(apiInfos)){
            String member= "";
            double count=0;
            for (Map<String,Object> api:apiInfos) {
                member = (api.get("url")+"").replaceFirst("\\/", "").replace("/", ":");
                count = CacheZSet.zscore(PER_API_VISITCOUNT,member);
                if(count<0){
                    count=0;
                }
                api.put("count",count);
            }
            Collections.sort(apiInfos, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    double diff = Double.parseDouble(o1.get("count")+"") - Double.parseDouble(o2.get("count")+"");
                    if (diff < 0) {
                     return 1;
                   }else if (diff > 0) {
                     return -1;
                   }
                    return 0;
                }
            });
        }
        return apiInfos;
    }

    @Override
    public List<Map<String, Object>> getMartBasicCount() {
        return dataMartMapper.getMartBasicCount();
    }
}
